﻿using System;
using System.Windows.Media.Imaging;

namespace ImageProcessing.Common
{
    public static class Wp7HelperMethods
    {
        public static ExtractedImage ExtractPointsFromResource(int width, int height, string resource, string imageName)
        {
            var uri = new Uri(resource, UriKind.Relative);
            var bi = new BitmapImage(uri) { CreateOptions = BitmapCreateOptions.None };
            var writeableBitmap = new WriteableBitmap(bi);

            var switchWidthHeight = width > height && writeableBitmap.PixelHeight > writeableBitmap.PixelWidth;

            if (switchWidthHeight)
            {
                var tempW = width;
                var tempH = height;

                height = tempW;
                width = tempH;
            }

            var resizedBitmap = writeableBitmap.Resize(width, height, WriteableBitmapExtensions.Interpolation.Bilinear);
            return new ExtractedImage(width, height, resizedBitmap.Pixels, imageName);
        }
    }
}
